﻿using System.Text;

using Cognifide.PowerShell.Shell.Host;
using Cognifide.PowerShell.Shell.Settings;

using Sitecore;
using Sitecore.Diagnostics;
using Sitecore.Web.UI.HtmlControls;
using Sitecore.Web.UI.Sheer;
using System;
using Cognifide.PowerShell.Shell;

using System.Web;

namespace Cognifide.PowerShell.Console.Layouts
{
    public class PowerShellConsoleForm : global::Sitecore.Web.UI.Sheer.BaseForm
    {
        //Memo txtResult;
        Memo txtScript=null;
        Literal txtResult=null;

        public const string VERSION_STRING = "Sitecore Powershell Console\r\nCopyright (c) 2010-2011 <a href='http://www.cognifide.com' target='_blank'>Cognifide Limited</a> & <a href='http://blog.najmanowicz.com/' target='_blank'>Adam Najmanowicz</a>. All rights Reserved<br/><br/>Enter your script/command and press Ctrl+Enter to execute your script.<br/><br/>";
        private PsConsoleSettings Settings { get { return PsConsoleSettings.Instance; } }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            txtResult.Text = VERSION_STRING;
            if (!Sitecore.Context.ClientPage.IsPostBack)
            {
                txtScript.Value = Settings.LastScript;
            }
            else
            {
                //Execute();
            }
        }
        public void Execute()
        {
            txtResult.Text = VERSION_STRING;
            using (var runspace = new ScriptSession())
            {
                try
                {
                    runspace.ExecuteScriptPart(PsConsoleSettings.Instance.PreScript);
                    runspace.ExecuteScriptPart(txtScript.Value);
                }
                catch (Exception exc)
                {
                    txtResult.Text += "<span style='background-color:black;color:red;'>" + runspace.GetExceptionString(exc).Replace("\r\n", "<br/>").Replace(" ", "&nbsp;") + "</span>";
                }

                StringBuilder output = new StringBuilder(10240);

                foreach (var outputLine in runspace.Output)
                {
                    output.Append(HttpUtility.HtmlEncode(outputLine.Text));
                }
                txtResult.Text += output.ToString();
                //txtResult.Text += "<span style='background-color:black;color:red;'>" + runspace.Errors.Replace("\r\n", "<br/>").Replace(" ", "&nbsp;") + "</span>";
            }

            if (Settings.SaveLastScript)
            {
                Settings.Load();
                Settings.LastScript = txtScript.Value;
                Settings.Save();
            }
            /*
            //plhOutput.Visible = !String.IsNullOrEmpty(litVars.Text) || !String.IsNullOrEmpty(litOutput.Text);
            plhProblems.Visible = !String.IsNullOrEmpty(litExceptions.Text) || !String.IsNullOrEmpty(litErrors.Text);
            plhException.Visible = !String.IsNullOrEmpty(litExceptions.Text);
            //plhOutput.Visible = !plhException.Visible;
            plhErrors.Visible = !String.IsNullOrEmpty(litErrors.Text);
             */
        }

    }
}
